nice_things/lang/assign_variable.sh
assign_variable
Since 0.3.0 · Source
import "{ assign_variable }" from nice_things/lang/assign_variable.sh
Synopsisassign_variable <variable_assignment>
Configuration
–
Description
Use indirection to dynamically assign a variable. The argument <variable_assignment> must be in the form <name>=<value>, where <name> will be checked if it is a valid shell name, otherwise an error status code is returned.
In application code where the inputs are trusted, you may opt for the var macro instead, which generates faster code, but does not validate inputs.
Warning
Note that even with checks to verify that
<name>is a valid name for a shell variable, ensuring this function will not accidentally execute its parameters as code instead of making a variable assignment, using untrusted inputs is still not completely safe. Any variable can be assigned with this function, which may affect the program's (and even the shell's) internal state in unpredictable ways.
Options
–
Operands<variable_assignment>: A variable assignment in the format <name>=<value>.
Stdin
–
Stdout
–
Stderr
–
Exit status
0: Successful completion.11: Invalid or missing arguments (abort).26: Invalid variable name.123: Unexpected error (abort).
Abort
- Aborts on invalid or missing arguments.
- Aborts on unexpected error.
Usage examples
# Assign the result of a function to the <out_var> parameter
assign_variable "${1}=${result}" || abort "Invalid variable name '${1}'"